package com.hlf.modules.wechat.service.impl;

import com.google.common.collect.Maps;
import com.hlf.modules.wechat.mapper.ConfigWechatMapper;
import com.hlf.modules.wechat.model.ConfigWechat;
import com.hlf.modules.wechat.model.ConfigWechatTemplate;
import com.hlf.modules.wechat.model.ConfigWechatVO;
import com.hlf.modules.wechat.service.ConfigWechatService;
import com.hlf.modules.wechat.service.ConfigWechatTemplateService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * ConfigWechatService实现
 * @since 0.0.1
 * @author pan
 * @date 2017-07-07
 *
 */
@Service
public class ConfigWechatServiceImpl implements ConfigWechatService {

    @Autowired
    private ConfigWechatMapper mapper;
    
    @Autowired
    private ConfigWechatTemplateService configWechatTemplateService;

    @Override
    public int deleteById(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(ConfigWechat record) {
        return mapper.insertSelective(record);
    }

    @Override
    public ConfigWechat selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public ConfigWechat selectByAppId(String appId) {
        return mapper.selectByAppId(appId);
    }

    @Override
    public List<ConfigWechat> select() {
        return mapper.select();
    }

    @Override
    public List<ConfigWechat> selectByAgentId(Long topAgentId) {
        return mapper.selectByAgentId(topAgentId);
    }

    @Override
    public int updateById(ConfigWechat record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public ConfigWechat selectByAgentIdAndBizType(Long topAgentId) {
        ConfigWechat config = new ConfigWechat();
        config.setTopAgentId(topAgentId);
//        config.setBizType(bizType);
        return mapper.selectByAgentIdAndBizType(config);
    }
    
    @Override
    public ConfigWechatVO selectConfigVOByAgentIdAndBizType(Long topAgentId) {
        ConfigWechat config = new ConfigWechat();
        config.setTopAgentId(topAgentId);
//        config.setBizType(bizType);
        
        ConfigWechatVO configWechatVO = new ConfigWechatVO();
        ConfigWechat configWechat = selectByAgentIdAndBizType(topAgentId);
        if(configWechat!=null){
            List<ConfigWechatTemplate> wechatTemplates = configWechatTemplateService.selectByAgentId(configWechat.getTopAgentId());
            Map<String, ConfigWechatTemplate> wechatTemplate = Maps.newHashMap();
            for (ConfigWechatTemplate configWechatTemplate : wechatTemplates) {
                wechatTemplate.put(configWechatTemplate.getTemplateCode(), configWechatTemplate);
            }
            BeanUtils.copyProperties(configWechat, configWechatVO);
            configWechatVO.setWechatTemplate(wechatTemplate);
        }
        return configWechatVO;
    }

    @Override
    public ConfigWechatVO selectMerchantConfigByAgentId(Long topAgentId) {
        return selectConfigVOByAgentIdAndBizType(topAgentId);
    }

    @Override
    public ConfigWechatVO selectPersonalConfigByAgentId(Long topAgentId) {
        return selectConfigVOByAgentIdAndBizType(topAgentId);
    }

}